package com.mp.approvalreq.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mp.approvalreq.entity.Request;
import com.mp.approvalreq.service.ApprovalOperateServicePrx;
import com.mp.approvalreq.util.ServiceUtils;
import com.mp.user.entity.User;
import com.plusmoney.exception.RpcServerLogicalException;
import com.plusmoney.util.Utils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 临时添加抄送人
 *
 * @author Administrator
 */
@WebServlet("/addTempCc")
public class AddTempCc extends AbstractServlet {

    private static final long serialVersionUID = -6524473373623928868L;

    private static final Logger logger = LoggerFactory.getLogger(AddTempCc.class);


    /**
     * 数据需要为：{ requestDkey:4, templateId:1, applicant:84, ccTemp:临时抄送人contactid }
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
     throws ServletException, IOException {
        try {
            User user = getReqUser(req);

            Request request = getRequest(req);

            check(request);

            request.setUser(user);

            ApprovalOperateServicePrx approvalOperateServicePrx = ServiceUtils.getApprovalOperateServicePrx();
            approvalOperateServicePrx.addTempCc(request);

            printResult(resp);
        } catch (Throwable e) {
            onThrowable(req, resp, e);
        }
    }

    private void printResult(HttpServletResponse resp) throws IOException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", "0");
        jsonObject.put("res_info", "添加成功");

        logger.info(JSON.toJSONString(jsonObject));

        printJson(resp, jsonObject.toJSONString());
    }


    private Request getRequest(HttpServletRequest req) throws IOException {
        String reqBody = getContent(req);
        logger.info("json参数值: {}", reqBody);

        Request request;
        try {
            request = JSONObject.parseObject(reqBody, Request.class);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new RpcServerLogicalException(1, "请求参数格式错误");
        }

        return request;
    }

    protected void check(Request request) {
        if (request.getTemplateId() == 0) {
            throw new RpcServerLogicalException(1, "模板参数不正确");
        }

        if (StringUtils.isEmpty(request.getCcTemp())) {
            throw new RpcServerLogicalException(1, "请求内容为空");
        }

        if (Utils.string2list(request.getCcTemp()).size() > 30) {
            throw new RpcServerLogicalException(1, "最多添加30个抄送人");
        }

        if (request.getRequestDkey() == 0) {
            throw new RpcServerLogicalException(1, "审批id不能为空");
        }

    }

}
